<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleCreate"> 新增</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon style="fontSize:12px" icon="ant-design:down-outlined"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)"/>
      </template>
    </BasicTable>
    <DetailModal @register="register" />
    <MchDrawer @register="registerMchDrawer" @success="reload" />
  </div>
</template>
<script lang="ts" name="PayMchInfo" setup>
import { ref } from 'vue';
import { BasicTable, TableAction } from '/@/components/Table';
import { useDrawer } from '/@/components/Drawer';
import DetailModal from './DetailModal.vue';
import MchDrawer from './MchInfoDrawer.vue';
import { useGlobSetting } from '/@/hooks/setting';
import { columns, searchFormSchema } from './mchInfo.data';
import { listMchInfo, deleteMchInfo, batchDeleteMchInfo } from './mchInfo.api';
import { useListPage } from '/@/hooks/system/useListPage';
import { useMessage } from '/@/hooks/web/useMessage';
import { useRoute, useRouter } from "vue-router"
import { Icon } from '/@/components/Icon'

const { createMessage } = useMessage();
const router = useRouter()

const glob = useGlobSetting();
const [register, { openDrawer: openDetail }] = useDrawer();
const [registerMchDrawer, { openDrawer: openMchDrawer }] = useDrawer();
// 列表页面公共参数、方法
const { prefixCls, tableContext } = useListPage({
  designScope: 'pay-mch-info',
  tableProps: {
    title: '商户列表',
    api: listMchInfo,
    columns: columns,
    bordered: false,
    actionColumn: {
      width: 185,
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      slots: { customRender: 'action' },
    },
    formConfig: {
      schemas: searchFormSchema,
      fieldMapToTime: [['fieldTime', ['beginDate', 'endDate'], 'YYYY-MM-DD HH:mm:ss']],
    },
  }
})

const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

/**
 * 查看
 */
function handleDetail(record) {
  openDetail(true, {
    record,
    isUpdate: true,
  });
}
/**
 * 编辑
 */
function handleEdit(record: Recordable) {
  openMchDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleGoMchApp(record: Recordable) {
  router.push({
    name: '/pay/mchApp',
    query: {
      mchNo: record.mchNo,
    }
  })
}

/**
 * 操作列定义
 * @param record
 */
function getActions(record) {
  return [
  {
      label: '删除',
      ifShow: !record.bindTenant,
      color: 'error',
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      },
    },
    {
      label: '查看',
      onClick: handleDetail.bind(null, record),
      // ifShow: record.sendStatus == 0
    },
    {
      label: '应用',
      onClick: handleGoMchApp.bind(null, record),
      // ifShow: record.sendStatus == 0
    },
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
      // ifShow: record.sendStatus == 0
    }
  ]
}

/**
 * 删除
 */
const handleDelete = async (record) => {
  await deleteMchInfo({ mchNo: record.mchNo }, () => {
    reload()
    createMessage.success("删除成功")
  })
  
}
/**
 * 批量删除事件
 */
 async function batchHandleDelete() {
  await batchDeleteMchInfo({ ids: selectedRowKeys.value }, () => {
    reload()
    createMessage.success("删除成功")
  });
}


/**
 * 新增
 */
 function handleCreate() {
  openMchDrawer(true, {
    isUpdate: false,
  });

  
}

</script>
